﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using dal;
using System.Collections;
using DAL;
using entity;
using System.Data;
using System.Net;
using System.Text;

/// <summary>
///  行情业务
/// </summary>
public class HangQingBLL
{
    public string GetHangYeData()
    {
        string key = "HangQingBLL_GetHangYeData";
        string str = (string)MemcachedTools.Get(key);
        if (!string.IsNullOrEmpty(str))
        {
            return str;
        }
        else
        {
            var dd = MemcachedTools.GetHangqingStr("EMONEY_DATA_1.0_AGENCIES_GROUP_DOWN");
            dd = dd.Replace(":,", ":null,");
            dd = dd.Replace("依米康?,\"", "依米康\",\"");
            var cc = MemcachedTools.GetHangqingStr("EMONEY_DATA_1.0_TOP_GOODS_SHSZA_DOWN");
            cc = cc.Replace(":,", ":null,");
            cc = cc.Replace("依米康?,\"", "依米康\",\"");
            var data1 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HangYeBK>>(dd);
            var data2 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(cc);
            Dictionary<string, StockPrice> dicStockPrice = new Dictionary<string, StockPrice>();
            foreach (var c in data2)
            {
                if (!dicStockPrice.Keys.Contains(c.C))
                {
                    dicStockPrice.Add(c.C, c);
                }
            }
            CommonBLL cBll = new CommonBLL();
            YZHBGDAL ybll = new YZHBGDAL();
            DAL.QgqpDAL qgqpDal = new DAL.QgqpDAL();
            for (int i = 0; i < data1.Count; i++)
            {
                int bcount = 0;
                int scount = 0;
                int count = 0;
                int upcount = 0;
                int downcount = 0;
                var lst = cBll.LstStockType.Where(x => x.BK_CODE == data1[i].C);
                foreach (var c in lst)
                {
                    try
                    {
                        var s = dicStockPrice[c.GG_CODE];
                        count++;
                        if (s.NC > 0)
                        {
                            bcount++;
                        }
                        if (s.NC < 0)
                        {
                            scount++;
                        }
                        if (s.D > 0)
                        {
                            upcount++;
                        }
                        if (s.D < 0)
                        {
                            downcount++;
                        }
                    }
                    catch
                    {
                    }
                }
                data1[i].BCount = bcount;
                data1[i].SCount = scount;
                data1[i].Count = count;
                data1[i].UpCount = upcount;
                data1[i].DownCount = downcount;
                data1[i].Other = new HangYeBK_Other();
                var tzyd = cBll.LstInvestmentHighlights.Find(x => x.BKID == data1[i].C);
                string strTzyd = tzyd == null ? "" : tzyd.Point;
                var hyxz = cBll.LstIndustryStatus.Find(x => x.BKID == data1[i].C);
                string strHyxz = hyxz == null ? "" : hyxz.Now;

                data1[i].Other.Hyxw = qgqpDal.Get行业新闻(data1[i].C);
                data1[i].Other.Hyzk = strHyxz;
                data1[i].Other.Tzyd = strTzyd;

                data1[i].Other.Stocks = new List<HyStock>();
                DataTable tbs = ybll.Get可关注股票(data1[i].C);
                if (tbs != null)
                {
                    for (int j = 0; j < tbs.Rows.Count; j++)
                    {
                        string stockId = tbs.Rows[j]["stock_id"].ToString();
                        string stockName = cBll.DicStock[stockId];
                        data1[i].Other.Stocks.Add(new HyStock() { Code = stockId, StockName = stockName });
                    }
                }
            }
            str = Newtonsoft.Json.JsonConvert.SerializeObject(data1);
            MemcachedTools.Add(key, str, DateTime.Now.AddSeconds(120));
            return str;
        }
    }

    public string GetBKPrice(string[] bkid)
    {
        bkid = bkid.Select(x => "EMONEY_DATA_1.0_QUOTES_GROUP_" + x).ToArray();
        object[] objs = MemcachedTools.GetHangqing(bkid);
        ArrayList lst = new ArrayList();
        foreach (var c in objs)
        {
            if (c == null)
            {
                continue;
            }
            var d = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(c.ToString());
            lst.Add(d[0]);
        }
        return Newtonsoft.Json.JsonConvert.SerializeObject(lst);
    }

    public List<StockPrice> GetBKPrice2(string[] bkid)
    {
        bkid = bkid.Select(x => "EMONEY_DATA_1.0_QUOTES_GROUP_" + x).ToArray();
        object[] objs = MemcachedTools.GetHangqing(bkid);
        List<StockPrice> lst = new List<StockPrice>();
        foreach (var c in objs)
        {
            if (c == null)
            {
                continue;
            }
            var d = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(c.ToString());
            lst.Add(d[0]);
        }
        return lst;
    }

    public List<StockPrice> GetStockPrice(string[] stocks)
    {
        for (int i = 0; i < stocks.Length; i++)
        {
            stocks[i] = "EMONEY_DATA_1.0_QUOTES_GOODS_" + stocks[i];
        }
        object[] objs = MemcachedTools.GetHangqing(stocks);
        List<StockPrice> lst = new List<StockPrice>();
        for (int i = 0; i < objs.Length; i++)
        {
            string temp = (string)objs[i];
            if (string.IsNullOrEmpty(temp))
            {
                continue;
            }
            try
            {
                temp = temp.Replace("依米康?,\"", "依米康\",\"");
                var c = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<StockPrice>>(temp);
                if (c.Count > 0)
                {
                    lst.Add(c[0]);
                }
            }
            catch (Exception ex)
            {

            }
        }
        return lst;
    }

    public string 个股排行()
    {
        string key = "HangQingBLL_个股排行";
        string tt = (string)MemcachedTools.Get(key);
        if (!string.IsNullOrEmpty(tt))
        {
            return tt;
        }

        string hs300 = "001009";
        string cps80 = "001018";
        int count = 7;
        ArrayList 沪A = new ArrayList();
        ArrayList 深A = new ArrayList();
        ArrayList 沪深300 = new ArrayList();
        ArrayList 中小板 = new ArrayList();
        ArrayList 创业板 = new ArrayList();
        ArrayList 操盘手80 = new ArrayList();

        var tempLst = GetStockPriceList().Values.ToList();
        CommonBLL cbll = new CommonBLL();
        QgqpDAL qgqpDAL = new QgqpDAL();
        for (int i = 0; i < tempLst.Count; i++)
        {
            var c = tempLst[i];
            if (沪A.Count < count)
            {
                if (c.C.StartsWith("6"))
                {
                    DataRow row = qgqpDAL.GetQgqp(c.C);
                    decimal? score = null;
                    string qgqp = string.Empty;
                    if (row != null)
                    {
                        try
                        {
                            score = Tools.GetDecimal(row["total_score"]);
                            qgqp = cbll.Dic第一财经一句话股评[c.C];
                        }
                        catch
                        {
                        }
                    }
                    沪A.Add(new StockRanking { StockId = c.C, StockName = cbll.DicStock[c.C], Tjpf = score, Qgqp = qgqp });
                }
            }
            if (深A.Count < count)
            {
                DataRow row = qgqpDAL.GetQgqp(c.C);
                decimal? score = null;
                string qgqp = string.Empty;
                if (row != null)
                {
                    try
                    {
                        score = Tools.GetDecimal(row["total_score"]);
                        qgqp = cbll.Dic第一财经一句话股评[c.C];
                    }
                    catch
                    {
                    }
                }
                if (c.C.StartsWith("000"))
                {
                    深A.Add(new StockRanking { StockId = c.C, StockName = cbll.DicStock[c.C], Tjpf = score, Qgqp = qgqp });
                }
            }
            if (沪深300.Count < count)
            {
                DataRow row = qgqpDAL.GetQgqp(c.C);
                decimal? score = null;
                string qgqp = string.Empty;
                if (row != null)
                {
                    try
                    {
                        score = Tools.GetDecimal(row["total_score"]);
                        qgqp = cbll.Dic第一财经一句话股评[c.C];
                    }
                    catch
                    {
                    }
                }
                if (cbll.LstStockType.Any(x => x.BK_CODE == hs300 && x.GG_CODE == c.C))
                {
                    沪深300.Add(new StockRanking { StockId = c.C, StockName = cbll.DicStock[c.C], Tjpf = score, Qgqp = qgqp });
                }
            }
            if (中小板.Count < count)
            {
                DataRow row = qgqpDAL.GetQgqp(c.C);
                decimal? score = null;
                string qgqp = string.Empty;
                if (row != null)
                {
                    try
                    {
                        score = Tools.GetDecimal(row["total_score"]);
                        qgqp = cbll.Dic第一财经一句话股评[c.C];
                    }
                    catch
                    {
                    }
                }
                if (c.C.StartsWith("002"))
                {
                    中小板.Add(new StockRanking { StockId = c.C, StockName = cbll.DicStock[c.C], Tjpf = score, Qgqp = qgqp });
                }
            }
            if (创业板.Count < count)
            {
                DataRow row = qgqpDAL.GetQgqp(c.C);
                decimal? score = null;
                string qgqp = string.Empty;
                if (row != null)
                {
                    try
                    {
                        score = Tools.GetDecimal(row["total_score"]);
                        qgqp = cbll.Dic第一财经一句话股评[c.C];
                    }
                    catch
                    {
                    }
                }
                if (c.C.StartsWith("300"))
                {
                    创业板.Add(new StockRanking { StockId = c.C, StockName = cbll.DicStock[c.C], Tjpf = score, Qgqp = qgqp });
                }
            }
            if (操盘手80.Count < count)
            {
                DataRow row = qgqpDAL.GetQgqp(c.C);
                decimal? score = null;
                string qgqp = string.Empty;
                if (row != null)
                {
                    try
                    {
                        score = Tools.GetDecimal(row["total_score"]);
                        qgqp = cbll.Dic第一财经一句话股评[c.C];
                    }
                    catch
                    {
                    }
                }
                if (cbll.LstStockType.Any(x => x.BK_CODE == cps80 && x.GG_CODE == c.C))
                {
                    操盘手80.Add(new StockRanking { StockId = c.C, StockName = cbll.DicStock[c.C], Tjpf = score, Qgqp = qgqp });
                }
            }
            if (沪A.Count >= count && 深A.Count >= count && 沪深300.Count >= count && 中小板.Count >= count && 创业板.Count >= count && 操盘手80.Count >= count)
            {
                break;
            }
        }
        var data = new { 沪A = 沪A, 深A = 深A, 沪深300 = 沪深300, 中小板 = 中小板, 创业板 = 创业板, 操盘手80 = 操盘手80 };
        tt = Newtonsoft.Json.JsonConvert.SerializeObject(data);
        MemcachedTools.Add(key, tt, DateTime.Now.AddMinutes(2));
        return tt;
    }

    /// <summary>
    ///  根据板块编号--排序按照得分的逆序排列
    /// </summary>
    /// <param name="bkid">板块编号</param>
    /// <returns>股票代码</returns>
    public string GetStocksByPf(string bkid)
    {
        string key = "HangQingBLL_GetStocksByPf:" + bkid;
        string str = (string)MemcachedTools.Get(key);
        if (!string.IsNullOrEmpty(str))
        {
            return str;
        }
        QgqpDAL dal = new QgqpDAL();
        var stocks = dal.GetStocksByPf(bkid);
        if (stocks.Count == 0)
        {
            return string.Empty;
        }
        string[] keys = new string[stocks.Count];
        for (int i = 0; i < keys.Length; i++)
        {
            keys[i] = "EMONEY_DATA_1.0_QUOTES_GOODS_" + stocks[i];
        }
        var datas = MemcachedTools.GetHangqing(keys);
        ArrayList lst = new ArrayList();
        for (int i = 0; i < datas.Length; i++)
        {
            if (datas[i] == null)
            {
                continue;
            }
            var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(datas[i].ToString());
            lst.Add(new { temp[0].C, temp[0].N, temp[0].NC, temp[0].F });
        }
        if (lst.Count > 0)
        {
            str = Newtonsoft.Json.JsonConvert.SerializeObject(lst);
            MemcachedTools.Add(key, str, DateTime.Now.AddMinutes(1));
            return str;
        }
        else
        {
            return string.Empty;
        }
    }

    /// <summary>
    /// 行业板块涨幅榜
    /// </summary>
    /// <param name="count"></param>
    /// <returns></returns>
    public string BanKuaiZhangFu(int count = 3)
    {
      
            string key = "HangQingBLLBanKuaiZhangFu" + count.ToString();
            string str = "";
            string[] serverlist = System.Configuration.ConfigurationManager.AppSettings["bkzfMemcache"].Split(',');
            BeIT.MemCached.MemcachedClient.Setup("BanKuaiZhangFu", serverlist);
            BeIT.MemCached.MemcachedClient cache = BeIT.MemCached.MemcachedClient.GetInstance("BanKuaiZhangFu");
            cache.SendReceiveTimeout = 5000;
            cache.MinPoolSize = 1;
            cache.MaxPoolSize = 5;
            byte[] btGet = cache.Get("EMONEY_DATA_1.0_TOP_GROUP_HY_DOWN") as byte[];
            string str1 = "";
            if (btGet != null)
                str1 = Encoding.Default.GetString(btGet);
            if (string.IsNullOrEmpty(str1))
                return "暂无数据";
            List<StockPrice> lsts = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(str1);
            lsts = lsts.Where(x=>x.F.HasValue).OrderByDescending(p => p.F).Take(count).ToList();
            str = Newtonsoft.Json.JsonConvert.SerializeObject(lsts);
            return str;

    }

    public string 板块排行(int count = 7)
    {
        string key = "HangQingBLL_板块排行_" + count.ToString();
        string str = (string)MemcachedTools.Get(key);
        if (!string.IsNullOrEmpty(str))
        {
            return str;
        }

        //行业板块数据
        string str1 = MemcachedTools.GetHangqingStr("EMONEY_DATA_1.0_AGENCIES_GROUP_DOWN");
        //概念板块数据
        string str2 = MemcachedTools.GetHangqingStr("EMONEY_DATA_1.0_TOP_GROUP_GN_DOWN");
        //地区板块数据
        string str3 = MemcachedTools.GetHangqingStr("EMONEY_DATA_1.0_TOP_GROUP_DQ_DOWN");
        str1 = str1.Replace(":,", ":null,");
        str2 = str2.Replace(":,", ":null,");
        str3 = str3.Replace(":,", ":null,");
        List<StockPrice> lst行业 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(str1);
        List<StockPrice> lst概念 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(str2);
        List<StockPrice> lst地区 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(str3);
        lst行业 = lst行业.Take(count).ToList();
        lst概念 = lst概念.Take(count).ToList();
        lst地区 = lst地区.Take(count).ToList();

        List<StockPrice> lstStocks = GetStockPriceList().Values.ToList();
        Dictionary<string, StockPrice> dicStocks = new Dictionary<string, StockPrice>();
        foreach (var c in lstStocks)
        {
            try
            {
                dicStocks.Add(c.C, c);
            }
            catch
            {
            }
        }
        CommonBLL cBll = new CommonBLL();
        List<TopPlate> tlst行业 = new List<TopPlate>();
        foreach (var c in lst行业)
        {
            var lst = cBll.LstStockType.Where(x => x.BK_CODE == c.C).ToList();
            TopPlate t = new TopPlate();
            t.BKCode = c.C;
            try
            {
                t.BKName = cBll.DicType[c.C].BK_NAME;
            }
            catch
            {
            }
            t.NC = Convert.ToString(c.NC);
            t.StockCount = lst.Count;
            t.LZCode = lst[0].GG_CODE;
            t.LDCode = lst[0].GG_CODE;
            t.A = c.A;
            t.F = c.F;
            t.F5 = c.F5;
            t.I = c.I;
            t.QR = c.QR;
            t.J = c.J;
            for (int i = 0; i < lst.Count; i++)
            {
                try
                {
                    var temp = lst[i];
                    var temp1 = dicStocks[temp.GG_CODE].F;
                    if (temp1 >= 0)
                    {
                        t.UpCount++;
                    }
                    if (temp1 < 0)
                    {
                        t.DownCount++;
                    }
                    if (temp1 > dicStocks[t.LZCode].F)
                    {
                        t.LZCode = temp.GG_CODE;
                    }
                    if (temp1 < dicStocks[t.LDCode].F)
                    {
                        t.LDCode = temp.GG_CODE;
                    }
                }
                catch
                {
                }
            }
            try
            {
                t.LDName = cBll.DicStock[t.LDCode];
            }
            catch
            {
            }
            try
            {
                t.LZName = cBll.DicStock[t.LZCode];
            }
            catch
            {
            }
            tlst行业.Add(t);
        }

        List<TopPlate> tlst概念 = new List<TopPlate>();
        foreach (var c in lst概念)
        {
            var lst = cBll.LstStockType.Where(x => x.BK_CODE == c.C).ToList();
            TopPlate t = new TopPlate();
            t.BKCode = c.C;
            try
            {
                t.BKName = cBll.DicType[c.C].BK_NAME;
            }
            catch
            {
            }
            t.NC = Convert.ToString(c.NC);
            if (lst.Count == 0)
            {
                continue;
            }
            t.StockCount = lst.Count;
            t.LZCode = lst[0].GG_CODE;
            t.LDCode = lst[0].GG_CODE;
            t.A = c.A;
            t.F = c.F;
            t.F5 = c.F5;
            t.I = c.I;
            t.J = c.J;
            t.QR = c.QR;
            for (int i = 0; i < lst.Count; i++)
            {
                try
                {
                    var temp = lst[i];
                    decimal? temp1 = 0;
                    try
                    {
                        temp1 = dicStocks[temp.GG_CODE].F;
                    }
                    catch
                    {
                        continue;
                    }
                    if (temp1 >= 0)
                    {
                        t.UpCount++;
                    }
                    if (temp1 < 0)
                    {
                        t.DownCount++;
                    }
                    if (temp1 > dicStocks[t.LZCode].F)
                    {
                        t.LZCode = temp.GG_CODE;
                    }
                    if (temp1 < dicStocks[t.LDCode].F)
                    {
                        t.LDCode = temp.GG_CODE;
                    }
                }
                catch
                {
                }
            }
            try
            {
                t.LDName = cBll.DicStock[t.LDCode];
            }
            catch
            {
            }
            try
            {
                t.LZName = cBll.DicStock[t.LZCode];
            }
            catch
            {
            }
            tlst概念.Add(t);
        }

        List<TopPlate> tlst地区 = new List<TopPlate>();
        foreach (var c in lst地区)
        {
            var lst = cBll.LstStockType.Where(x => x.BK_CODE == c.C).ToList();
            TopPlate t = new TopPlate();
            t.BKCode = c.C;
            try
            {
                t.BKName = cBll.DicType[c.C].BK_NAME;
            }
            catch
            {
                continue;
            }
            t.NC = Convert.ToString(c.NC);
            t.StockCount = lst.Count;
            t.LZCode = lst[0].GG_CODE;
            t.LDCode = lst[0].GG_CODE;
            t.A = c.A;
            t.F = c.F;
            t.F5 = c.F5;
            t.I = c.I;
            t.QR = c.QR;
            t.J = c.J;
            for (int i = 0; i < lst.Count; i++)
            {
                try
                {
                    var temp = lst[i];
                    var temp1 = dicStocks[temp.GG_CODE].F;
                    if (temp1 >= 0)
                    {
                        t.UpCount++;
                    }
                    if (temp1 < 0)
                    {
                        t.DownCount++;
                    }
                    if (temp1 > dicStocks[t.LZCode].F)
                    {
                        t.LZCode = temp.GG_CODE;
                    }
                    if (temp1 < dicStocks[t.LDCode].F)
                    {
                        t.LDCode = temp.GG_CODE;
                    }
                }
                catch
                {
                }
            }
            try
            {
                t.LDName = cBll.DicStock[t.LDCode];
            }
            catch
            {
            }
            try
            {
                t.LZName = cBll.DicStock[t.LZCode];
            }
            catch
            {
            }
            tlst地区.Add(t);
        }
        var data = new { 行业板块 = tlst行业, 概念板块 = tlst概念, 地域板块 = tlst地区 };
        str = Newtonsoft.Json.JsonConvert.SerializeObject(data);
        MemcachedTools.Add(key, str, DateTime.Now.AddSeconds(60));
        return str;
    }

    public string Get行业操盘摘要指数列表()
    {
        string key = "HangQingBLL.行业操盘摘要指数列表";
        string str = (string)MemcachedTools.Get(key);
        if (!string.IsNullOrEmpty(str))
        {
            return str;
        }
        string hs300 = "001009";

        str = MemcachedTools.GetHangqingStr("EMONEY_DATA_1.0_TOP_GOODS_SHSZA_DOWN");
        str = str.Replace(":,", ":null,");
        str = str.Replace("依米康?,\"", "依米康\",\"");
        var tempLst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(str);
        CommonBLL cbll = new CommonBLL();
        QgqpDAL qgqpDAL = new QgqpDAL();

        string strZs = MemcachedTools.GetHangqingStr("EMONEY_DATA_1.0_TOP_INDEX_DOWN");
        strZs = strZs.Replace(":,", ":null,");

        List<StockPrice> spLst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(strZs);
        //上证指数
        StockPrice spSz = spLst.Find(x => x.C == "000001");
        //深市
        StockPrice spSs = spLst.Find(x => x.C == "399001");
        //中小板
        StockPrice spZxb = spLst.Find(x => x.C == "399101");
        //创业版
        StockPrice spCyb = spLst.Find(x => x.C == "399006");
        //沪深300
        StockPrice spHs300 = spLst.Find(x => x.C == "399300");

        Cpzyzs cSz = new Cpzyzs() { code = "000001", J = spSz.J, F = spSz.F, NC = spSz.NC, scmc = spSz.N, P = spSz.P, D = spSz.D };
        Cpzyzs cSs = new Cpzyzs() { code = "399001", J = spSs.J, F = spSs.F, NC = spSs.NC, scmc = spSs.N, P = spSs.P, D = spSs.D };
        Cpzyzs cZxb = new Cpzyzs() { code = "399101", J = spZxb.J, F = spZxb.F, NC = spZxb.NC, scmc = spZxb.N, P = spZxb.P, D = spZxb.D };
        Cpzyzs cCyb = new Cpzyzs() { code = "399006", J = spCyb.J, F = spCyb.F, NC = spCyb.NC, scmc = spCyb.N, P = spCyb.P, D = spCyb.D };
        Cpzyzs cHs300 = new Cpzyzs() { code = "000300", J = spHs300.J, F = spHs300.F, NC = spHs300.NC, scmc = spHs300.N, P = spHs300.P, D = spHs300.D };
        for (int i = 0; i < tempLst.Count; i++)
        {
            var c = tempLst[i];
            //沪A
            if (c.C.StartsWith("6"))
            {
                cSz.gpCount++;
                if (c.F > 0)
                {
                    cSz.uCount++;
                }
                else if (c.F < 0)
                {
                    cSz.dCount++;
                }
            }


            //深A
            if (c.C.StartsWith("000"))
            {
                cSs.gpCount++;
                if (c.F > 0)
                {
                    cSs.uCount++;
                }
                else if (c.F < 0)
                {
                    cSs.dCount++;
                }
            }

            //沪深300
            if (cbll.LstStockType.Any(x => x.BK_CODE == hs300 && x.GG_CODE == c.C))
            {
                cHs300.gpCount++;
                if (c.F > 0)
                {
                    cHs300.uCount++;
                }
                else if (c.F < 0)
                {
                    cHs300.dCount++;
                }
            }

            //中小板
            if (c.C.StartsWith("002"))
            {
                cZxb.gpCount++;
                if (c.F > 0)
                {
                    cZxb.uCount++;
                }
                else if (c.F < 0)
                {
                    cZxb.dCount++;
                }
            }

            //创业板
            if (c.C.StartsWith("300"))
            {
                cCyb.gpCount++;
                if (c.F > 0)
                {
                    cCyb.uCount++;
                }
                else if (c.F < 0)
                {
                    cCyb.dCount++;
                }
            }


        }
        ArrayList tlst = new ArrayList();
        tlst.Add(cSz);
        tlst.Add(cSs);
        tlst.Add(cHs300);
        tlst.Add(cZxb);
        tlst.Add(cCyb);
        string strT = Newtonsoft.Json.JsonConvert.SerializeObject(tlst);
        MemcachedTools.Add(key, strT, DateTime.Now.AddMinutes(2));
        return strT;
    }

    public List<StockPrice> Get指数(string[] codes)
    {
        string[] keys = new string[codes.Length];
        for (int i = 0; i < codes.Length; i++)
        {
            keys[i] = "EMONEY_DATA_1.0_QUOTES_INDEX_" + codes[i];
        }
        object[] objs = MemcachedTools.GetHangqing(keys);

        //EMONEY_DATA_1.0_QUOTES_INDEX_000001
        List<StockPrice> lst = new List<StockPrice>();
        for (int i = 0; i < objs.Length; i++)
        {
            if (objs[i] == null)
            {
                continue;
            }
            string str = objs[i].ToString();
            var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<List<StockPrice>>(str);
            lst.Add(temp[0]);
        }
        return lst;
    }

    public List<Ggph> Get股票排行_bkid(string bkid)
    {
        Dictionary<string, StockPrice> dicPrice = GetStockPriceList();
        CommonBLL cbll = new CommonBLL();
        string[] codes = cbll.LstStockType.Where(x => x.BK_CODE == bkid).Select(x => x.GG_CODE).ToArray();
        List<Ggph> lstData = new List<Ggph>();
        foreach (var c in codes)
        {
            try
            {
                var stockPrice = dicPrice[c];
                Ggph g = new Ggph();
                g.A = stockPrice.A;
                g.C = stockPrice.C;
                g.F = stockPrice.F;
                g.F5 = stockPrice.F5;
                g.Hymc = cbll.Dic股票对应行业类型[g.C].BK_NAME;
                g.I = stockPrice.I;
                g.N = stockPrice.N;
                g.P = stockPrice.P;
                g.Qgqp = cbll.Dic第一财经一句话股评[g.C];
                g.Score = cbll.Dic千股千评[g.C].TotalScore;
                lstData.Add(g);
            }
            catch
            {
            }
        }
        return lstData;
    }


    public List<Ggph> Get股票排行(string[] stocks)
    {
        Dictionary<string, StockPrice> dicPrice = GetStockPriceList();
        CommonBLL cbll = new CommonBLL();
        List<Ggph> lstData = new List<Ggph>();
        foreach (var c in stocks)
        {
            try
            {
                var stockPrice = dicPrice[c];
                Ggph g = new Ggph();
                g.A = stockPrice.A;
                g.C = stockPrice.C;
                g.F = stockPrice.F;
                g.F5 = stockPrice.F5;
                g.Hymc = cbll.Dic股票对应行业类型[g.C].BK_NAME;
                g.I = stockPrice.I;
                g.N = stockPrice.N;
                g.P = stockPrice.P;
                try
                {
                    g.Qgqp = cbll.Dic第一财经一句话股评[g.C];
                    g.Score = cbll.Dic千股千评[g.C].TotalScore;
                }
                catch
                {
                    g.Qgqp = "";
                    g.Score = 0;
                }
                lstData.Add(g);
            }
            catch
            {
            }
        }
        return lstData;
    }

    public List<Ggph> Get股票排行(string flag)
    {
        Dictionary<string, StockPrice> dicPrice = GetStockPriceList();
        CommonBLL cbll = new CommonBLL();
        CommonBLL.Zhishu lx = CommonBLL.Zhishu.沪A;
        switch (flag.ToLower())
        {
            case "ha":
                lx = CommonBLL.Zhishu.沪A;
                break;
            case "sa":
                lx = CommonBLL.Zhishu.深A;
                break;
            case "zxb":
                lx = CommonBLL.Zhishu.中小板;
                break;
            case "hs300":
                lx = CommonBLL.Zhishu.沪深300;
                break;
            case "cyb":
                lx = CommonBLL.Zhishu.创业板;
                break;
        }
        List<string> stocks = cbll.Dic指数相关股票[lx];
        List<Ggph> lstData = new List<Ggph>();
        foreach (var c in stocks)
        {
            try
            {
                var stockPrice = dicPrice[c];
                Ggph g = new Ggph();
                g.A = stockPrice.A;
                g.C = stockPrice.C;
                g.F = stockPrice.F;
                g.F5 = stockPrice.F5;
                g.Hymc = cbll.Dic股票对应行业类型[g.C].BK_NAME;
                g.I = stockPrice.I;
                g.N = stockPrice.N;
                g.P = stockPrice.P;
                try
                {
                    g.Qgqp = cbll.Dic第一财经一句话股评[g.C];
                    g.Score = cbll.Dic千股千评[g.C].TotalScore;
                }
                catch
                {
                    g.Qgqp = "";
                    g.Score = 0;
                }
                lstData.Add(g);
            }
            catch
            {
            }
        }
        return lstData;

    }

    public string GetMaxPaihang(int top)
    {
        string key = "HangQingBLL.GetMaxPaihang." + top;
        string str = (string)MemcachedTools.Get(key);
        if (string.IsNullOrEmpty(str))
        {
            Dictionary<string, StockPrice> dicLst = GetStockPriceList();
            var lst = dicLst.Values.Where(x => x.C.Substring(0, 1) == "0" || x.C.Substring(0, 1) == "3" || x.C.Substring(0, 1) == "6").ToList();
            //涨幅
            var lst涨幅 = lst.Where(x => x.F != null).OrderByDescending(x => x.F).Take(top).ToList();
            //跌幅
            var lst跌幅 = lst.Where(x => x.F != null).OrderBy(x => x.F).Take(top).ToList();
            //振幅
            var lst振幅 = lst.Where(x => x.ZF != null).OrderByDescending(x => x.ZF).Take(top).ToList();
            //换手率
            var lst换手率 = lst.Where(x => x.I != null).OrderByDescending(x => x.I).Take(top).ToList();
            //成交金额
            var lst成交金额 = lst.Where(x => x.A != null).OrderByDescending(x => x.A).Take(top).ToList();
            var data = new { Lst涨幅 = lst涨幅, Lst跌幅 = lst跌幅, Lst振幅 = lst振幅, Lst换手率 = lst换手率, Lst成交金额 = lst成交金额 };
            str = JsonBLL.ToJson(data);
            MemcachedTools.Add(key, str, DateTime.Now.AddMinutes(1));
        }
        return str;
    }

    public Dictionary<string, StockPrice> GetStockPriceList()
    {
        string key = "HangQingBLL.StockPriceList";
        Dictionary<string, StockPrice> dic = (Dictionary<string, StockPrice>)System.Web.HttpRuntime.Cache[key];
        if (dic != null && dic.Count > 0)
        {
            return dic;
        }
        dic = new Dictionary<string, StockPrice>();
        CommonBLL cbll = new CommonBLL();
        var dicStock = cbll.DicStock;
        HangQingBLL hBll = new HangQingBLL();
        string[] keys = new string[dicStock.Keys.Count];
        int j = 0;
        foreach (var c in dicStock.Keys)
        {
            keys[j] = c;
            j++;
        }
        var lst = hBll.GetStockPrice(keys).ToList();
        for (int i = 0; i < lst.Count; i++)
        {
            try
            {
                dic.Add(lst[i].C, lst[i]);
            }
            catch
            {
            }
        }
        System.Web.HttpRuntime.Cache.Add(key, dic, null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, CacheStockPriceList);
        return dic;
    }

    void CacheStockPriceList(string key, object obj, System.Web.Caching.CacheItemRemovedReason c)
    {
        Dictionary<string, StockPrice> dic = new Dictionary<string, StockPrice>();
        CommonBLL cbll = new CommonBLL();
        var dicStock = cbll.DicStock;
        HangQingBLL hBll = new HangQingBLL();
        string[] keys = new string[dicStock.Keys.Count];
        int j = 0;
        foreach (var t in dicStock.Keys)
        {
            keys[j] = t;
            j++;
        }
        var lst = hBll.GetStockPrice(keys).ToList();
        for (int i = 0; i < lst.Count; i++)
        {
            try
            {
                dic.Add(lst[i].C, lst[i]);
            }
            catch
            {
            }
        }
        System.Web.HttpRuntime.Cache.Add(key, dic, null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, new HangQingBLL().CacheStockPriceList);
    }

}